<template>
	<view class="cart-container" v-if="cart.length !== 0">
		<my-address></my-address>
		<!-- 购物车图标 -->
		<view class="cart-title">
			<uni-icons type="shop" size="18"></uni-icons>
			<text>购物车</text>
		</view>


		<uni-swipe-action>
			<block v-for="(goods,i) in cart" :key="i">
				<uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)">
					<!-- 商品区 -->
					<my-goods :goods="goods" :showRadio="true" @radio-change="radioChangeHandler" :showNum="true"
						@num-change="numberChangeHandler" @xqclick="goodsXq"></my-goods>
					<!-- 	删除按钮 -->
					<template v-slot:right>
						<view class="slot-button" @click="swipeActionClickHandler(goods)">
							<text class="slot-button-text">删除</text>
						</view>
					</template>
				</uni-swipe-action-item>
			</block>
		</uni-swipe-action>
		<!-- 商品结算 -->
		<my-settle></my-settle>
	</view>
	<!-- 无商品的空白页面 -->
	<view class="empty-cart" v-else>
		<image src="/static/cart_empty@2x.png" class="empty-img"></image>
		<text class="tip-text">空空如也~</text>
	</view>
</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	import { mapState, mapMutations } from 'vuex'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		computed: { ...mapState('m_cart', ['cart']) },
		data() {
			return {
				options: [{
					text: '删除',
					style: { backgroundColor: '#C00000' }
				}],
			}

		},
		methods: {
			...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),
			// 商品的勾选状态发生了变化
			radioChangeHandler(e) {
				this.updateGoodsState(e)
			},
			// 购物车商品数量变化
			numberChangeHandler(e) {
				this.updateGoodsCount(e)
			},
			// 当前商品信息
			swipeActionClickHandler(goods) {
				this.removeGoodsById(goods.goods_id)
			},
			// 点击商品跳到商品详情
			goodsXq(goods) {
				uni.navigateTo({ url: `/subpkg/goods_detail/goods_detail?goods_id=${goods.goods_id}` })
			}
		}
	}
</script>

<style lang="scss">
	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		font-size: 14px;
		padding-left: 2px;
		border-bottom: 1px solid #efefef;

		text {
			margin-left: 5px;
		}
	}

	.slot-button {
		background-color: #C00000;
		display: flex;
		width: 150rpx;
		justify-content: center;
		align-items: center;

		.slot-button-text {
			color: #FFF;
		}
	}

	.cart-container {
		padding-bottom: 50px;
	}

	.empty-cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 150px;

		.empty-img {
			width: 90px;
			height: 90px;
		}

		.tip-text {
			font-size: 12px;
			color: #6c6c6c;
			margin-top: 15px;
		}
	}
</style>
